package nl.hanze.designpatterns.DAO.impl.file.ser;

import nl.hanze.designpatterns.DAO.QuestionDAO;
import nl.hanze.designpatterns.DAO.TroubleTicketDAO;
import nl.hanze.designpatterns.DAO.impl.file.BaseFDAOImpl;
import nl.hanze.designpatterns.Domain.Question;
import nl.hanze.designpatterns.Domain.TroubleTicket;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Aron on 6-10-2014.
 */
public class TroubleTicketFDAOImpl extends BaseFDAOImpl implements TroubleTicketDAO {

    private static final String path = new File("src/main/java/nl/hanze/designpatterns/files").getAbsolutePath().concat("/TroubleTicketsFile.file");

    @Override
    public List<TroubleTicket> getTroubleTickets() {
        return Deserialize();
    }

    @Override
    public List<TroubleTicket> getTroubleTicketsByName(String callerName) {
        List<TroubleTicket> troubleTickets = Deserialize();
        List<TroubleTicket> filteredTickets = new ArrayList<>();

        for (TroubleTicket ticket : troubleTickets) {
            if (ticket.getCallerName().equals(callerName)) {
                filteredTickets.add(ticket);
            }

        }
        return filteredTickets;
    }

    @Override
    public void saveTroubleTicket(TroubleTicket troubleticket) {
        int maxId = 0;
        List<TroubleTicket> troubleTickets = Deserialize();
        List<TroubleTicket> newTickets = new ArrayList<>();
        for (TroubleTicket ticket : troubleTickets) {
            if (maxId < ticket.getId()) {
                maxId = ticket.getId();
            }
            if (troubleticket.getId() != ticket.getId()) {
                newTickets.add(ticket);
            }
        }
        if (troubleticket.getId() == 0) {
            troubleticket.id = maxId+1;
        }
        newTickets.add(troubleticket);
        Serialize(newTickets);
    }

    public static void Serialize(List<TroubleTicket> troubleTickets) {
        try
        {
            FileOutputStream fileOut = new FileOutputStream(path);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(troubleTickets);
            out.close();
            fileOut.close();
        }catch(IOException i)
        {
            i.printStackTrace();
        }
    }

    public static List<TroubleTicket> Deserialize() {
        List<TroubleTicket> troubleTickets = null;
        try
        {
            FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            troubleTickets = (List<TroubleTicket>) in.readObject();
            in.close();
            fileIn.close();
            return troubleTickets;
        }catch(IOException i)
        {
            i.printStackTrace();
            return null;
        }catch(ClassNotFoundException c)
        {
            c.printStackTrace();
            return null;
        }
    }
}
